package com.ss.ttm.player;

import android.annotation.SuppressLint;
import android.content.Context;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.os.Build;

import com.ss.ttm.a.a;

import java.nio.ByteBuffer;

public class AJVoice {
    private static final int S_FAIL = -1;
    private static final int S_OK = 0;
    private static final String TAG;
    private static final int kNoSettingVolume = -1;
    private int mAudioFormat;
    private AudioManager mAudioManager;
    private AudioTrack mAudioTrack;
    private int mBlockSize;
    private int mChannels;
    private int mChannelsLayout;
    private float mLeftVolume;
    private int mMaxVolume;
    private long mNativeObject;
    private TTPlayer mPlayer;
    private int mSampleBytes;
    private int mSampleFormat;
    private int mSampleRate;

    static {
        TAG = AJVoice.class.getSimpleName();
    }

    public AJVoice() {

        this.mNativeObject = 0;
        this.mAudioTrack = null;
        this.mPlayer = null;
        this.mBlockSize = 2048;
        this.mSampleRate = 44100;
        this.mChannels = 2;
        this.mSampleBytes = 2;
        this.mSampleFormat = 0;
        this.mAudioManager = null;
        this.mChannelsLayout = 12;
        this.mAudioFormat = 2;
        this.mLeftVolume = -1f;
        this.mMaxVolume = 0;
    }

    public void close() {
        this.mAudioTrack.release();
    }

    public int getMaxVolume() {
        if (this.mMaxVolume == 0 && this.mAudioManager != null) {
            this.mMaxVolume = this.mAudioManager.getStreamMaxVolume(3);
        }

        return this.mMaxVolume;
    }

    public float getVolume() {
        float v0_1;
        int v1;
        int v3 = 3;
        int v0 = 0;
        if (this.mPlayer == null || this.mLeftVolume != -1f || this.mAudioManager == null) {
            v1 = 0;
        } else {
            v1 = this.mAudioManager.getStreamMaxVolume(v3);
            v0 = this.mAudioManager.getStreamVolume(v3);
        }

        if (v0 >= 0) {
            v0_1 = ((float) v0);
        } else if (v1 > 0) {
            v0_1 = ((float) (v1 / 4));
        } else {
            v0_1 = 10f;
        }

        return v0_1;
    }

    public int open(long arg9, TTPlayer arg11) {
        int v5;
        int v0;
        int v3 = 4;
        int v7 = -1;
        int v2 = 2;
        this.mPlayer = arg11;
        this.mAudioManager = (AudioManager) this.mPlayer.getContext().getSystemService(Context.AUDIO_SERVICE);
        if (this.mChannels == 1) {
            this.mChannelsLayout = v3;
        } else if (this.mChannels == v2) {
            this.mChannelsLayout = 12;
        } else {
            return v7;
        }
        if(this.mSampleBytes == 1) {
            this.mAudioFormat = v2;
        }
        else if(this.mSampleBytes == v2) {
            this.mAudioFormat = v2;
        }
        else if(this.mSampleBytes == v3) {
            this.mAudioFormat = v3;
        }
        else {
            return v7;
        }

        int v1 = AudioTrack.getMinBufferSize(this.mSampleRate, this.mChannelsLayout, this.mAudioFormat);
        if(v1 > this.mBlockSize) {
            v0 = v1 / this.mBlockSize * this.mBlockSize;
            if(v1 > v0) {
                v0 += this.mBlockSize;
            }

            v5 = v0;
        } else {
            v5 = this.mBlockSize;
        }

        this.mAudioTrack = new AudioTrack(3, this.mSampleRate, this.mChannelsLayout, this.mAudioFormat, v5, 1);
        if(this.mAudioTrack.setPositionNotificationPeriod(this.mBlockSize) != 0) {
            return v7;
        }

        if(this.mLeftVolume != -1f) {
            this.setVolume(this.mLeftVolume, this.mLeftVolume);
        }

        return 0;
    }

    public void setSampleInfo(int arg1, int arg2, int arg3, int arg4, int arg5, int arg6) {
        this.mSampleRate = arg2;
        this.mChannels = arg3;
        this.mBlockSize = arg4;
        this.mSampleBytes = arg6;
        this.mSampleFormat = arg1;
    }

    public void setVolume(float arg5, float arg6) {
        try {
            if (this.mAudioManager != null) {
                this.mAudioManager.setStreamVolume(3, ((int) arg5), 0);
            } else if (this.mAudioTrack != null) {
                if (Build.VERSION.SDK_INT >= 21) {
                    this.mAudioTrack.setVolume(arg5);
                } else {
                    this.mAudioTrack.setStereoVolume(arg5, arg6);
                }
            }

            this.mLeftVolume = arg5;
            return;
        } catch (Exception v0) {
            return;
        }
    }

    public int start() {
        this.mAudioTrack.play();
        return 0;
    }

    public void stop() {
        this.mAudioTrack.stop();
    }

    @SuppressLint(value = {"NewApi"})
    public int write(ByteBuffer arg5) {
        int v0 = -1;
        if (arg5 == null) {
            a.a(AJVoice.TAG, "buffer is nullpoint");
        } else if (this.mAudioTrack.write(arg5, 0, arg5.capacity()) == arg5.capacity()) {
            arg5.flip();
            v0 = 0;
        }

        return v0;
    }

    public int write(byte[] arg4, int arg5) {
        int v0 = -1;
        if (arg4 == null) {
            a.a(AJVoice.TAG, "buffer is nullpoint");
        } else if (this.mAudioTrack.write(arg4, 0, arg5) == arg5) {
            v0 = 0;
        }

        return v0;
    }
}

